package com.vitabuybuy.tv.ui.mine.talent.task

import android.os.Bundle
import androidx.lifecycle.Observer
import com.blankj.utilcode.util.ScreenUtils
import com.lcodecore.tkrefreshlayout.RefreshListenerAdapter
import com.lcodecore.tkrefreshlayout.TwinklingRefreshLayout
import com.vitabuybuy.tv.R
import com.vitabuybuy.tv.databinding.FragmentTaskPoolBinding
import com.vitabuybuy.tv.dialog.GoodsScreenPopup
import com.vitabuybuy.tv.dialog.TaskScreenPopup
import com.vitabuybuy.tv.dto.*
import com.library.common.base.BaseFragment
import com.library.common.base.MessageModel
import com.library.common.dto.MessageEvent
import com.vitabuybuy.tv.ui.mine.talent.task.viewmodel.TaskViewModel
import com.vitabuybuy.tv.utils.EventCode
import com.vitabuybuy.tv.utils.KeyBoardUtils
import kotlinx.android.synthetic.main.fragment_task_pool.*

class TaskPoolFragment : BaseFragment<TaskViewModel, FragmentTaskPoolBinding>() {
    override val layoutId: Int
        get() = R.layout.fragment_task_pool

    private var pageNumber = 1
    private var fansScopeDto = FansScopeDto()
    private var screenPopup: TaskScreenPopup? = null
    private var params = HashMap<String, Any>()
    private var brandId = -1
    private var type = ""
    private var pageMap = HashMap<String, Any>().apply {
        this["size"] = 10
        this["desc"] = "id"
    }

    override fun initView(savedInstanceState: Bundle?) {
        mBinding.viewModel = viewModel
    }

    override fun lazyLoadData() {
        viewModel.getFieldLabelList()
        searchCondition(pageNumber)
    }

    private fun showScreenPop() {
        screenPopup = TaskScreenPopup(
            mContext,
            type,
            viewModel.labellist.value,
            fansScopeDto
        ).apply {
            setAdjustInputMethod(false)
            height = ScreenUtils.getScreenHeight()
            showPopupWindow()
        }
        screenPopup?.onConfirm =
            { labelList: MutableList<FieldLabelDto>, fansScopeDto: FansScopeDto, type: String ->
                this.fansScopeDto = fansScopeDto
                viewModel.labellist.value = labelList
                viewModel.labellist.value?.run {
                    for (brand in this) {
                        if (brand.isChecked) {
                            brandId = brand.id
                        }
                    }
                }
                this.type = type
                pageNumber = 1
                searchCondition(pageNumber)
            }
        screenPopup?.onReset = {
            this.type = ""
            this.fansScopeDto = FansScopeDto()
            brandId = -1
            pageNumber = 1
            searchCondition(pageNumber)
        }
    }

    override fun eventHandling() {
        refresh.setOnRefreshListener(object : RefreshListenerAdapter() {
            override fun onLoadMore(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber++
                searchCondition(pageNumber)
            }

            override fun onRefresh(refreshLayout: TwinklingRefreshLayout?) {
                pageNumber = 1
                searchCondition(pageNumber)
            }
        })
        tvSearch.setOnClickListener {
            pageNumber = 1
            searchCondition(pageNumber)
        }
        ivScreen.setOnClickListener {
            if (!viewModel.labellist.value.isNullOrEmpty()) {
                showScreenPop()
            } else {
                viewModel.getFieldLabelList()
            }
        }
    }

    private fun searchCondition(
        pageNumber: Int
    ) {
        KeyBoardUtils.hideSoftKeyboard(mContext)
        pageMap["page"] = pageNumber
        params["pageable"] = pageMap
        val taskMap = HashMap<String, Any>()
        if (etSearch.text.toString().isNotEmpty())
            taskMap["title"] = etSearch.text.toString()
        if (type.isNotEmpty())
            taskMap["type"] = type
        if(etSearch.text.toString().isEmpty() &&
            type.isEmpty()){
            params.remove("task")
        }else{
            params["task"] = taskMap

        }
        if (fansScopeDto.limit == 0 && fansScopeDto.most == 0) {
            params.remove("fansQuantity")
        } else {
            params["fansQuantity"] =
                mapOf(Pair("start", fansScopeDto.limit), Pair("end", fansScopeDto.most))
        }
        if (brandId != -1) {
            params["label"] = mapOf(Pair("id", brandId))
        } else {
            params.remove("label")
        }
        viewModel.getTaskPoolData(pageNumber, params)
    }

    override fun handleEvent(msg: MessageModel) {
        when (msg.code) {
            0 -> {
                pageNumber = 1
                searchCondition(pageNumber)
            }
            1 -> {
                refresh.onLoad(msg.arg1)
            }
            2 -> {
                val item = msg.obj as TaskPoolDto
                if (item.type == "直播") {
                    LiveTaskDetailActivity.openActivity(mContext, item.id)
                } else {
                    PlantGrassTaskDetailActivity.openActivity(mContext, item.id)
                }
            }
        }
    }

    override fun onEvent(event: MessageEvent) {
        when (event.code) {
            EventCode.refush_task_list -> {
                pageNumber = 1
                searchCondition(pageNumber)
            }
        }
    }
}
